<?php
require_once('Core/Model/Function/Abstract.php');

class Core_Model_Function_User extends Core_Model_Function_Abstract
{
    protected $_tableUser = null;

    protected $_tableUserRole = null;
    
    public function __construct()
    {
        require_once('Core/Model/Table/User.php');
        $this->_tableUser = new Core_Model_Table_User;
        
        require_once('Core/Model/Table/User/Role.php');
        $this->_tableUserRole = new Core_Model_Table_User_Role;
    }

    public function getUserRole()
    {
        require_once('Zend/Auth.php');
        $auth = Zend_Auth::getInstance();

        if ($auth->hasIdentity()) {
            $userId = $auth->getIdentity()->user_id;
        
            $user = $this->_tableUser
                         ->find($userId)
                         ->current();
            
            $userRole = $user->findDependentRowset('Core_Model_Table_User_Role')
                             ->current()
                             ->user_role_name;
        } else {
            $where = $this->_tableUserRole->select()->where('user_role_parent = ?', 0);
            $result= $this->_tableUserRole->fetchRow($where);
            $userRole = $result['user_role_name'];
        }

        return $userRole;
    }





    public function getAllUsers()
    {
        $users = $this->_modelTableUser->fetchAll();
        return $users;
    }

    public function getUserByLogin($login)
    {
        $where = $this->_tableUser->select()->where('user_login = ?', $login);
        $data = $this->_tableUser->fetchRow($where);
        return $data;
    }

    public function getProfilerResult()
    {
        $fetchAll = $this->fetchAll();
        $fetchRow = $this->fetchRow();
        $fetchRow = $this->fetchNew();

        $profiler = $this->getAdapter()->getProfiler();

        $result = '';
        $result = 'total time : ' . $profiler->getTotalElapsedSecs() . '<br />';
        $result .= 'total queries : ' . $profiler->getTotalNumQueries() . '<br /><br />';

        $i = 1;
        foreach ($profiler->getQueryProfiles() as $query)
        {
            $result .= 'query : ' . $query->getQuery() . '<br />';
            $result .= 'query ' . $i++ . ' time : ' . $query->getElapsedSecs() . '<br />';
            $result .= 'query params : ' . nl2br(print_r($query->getQueryParams(), true)) . '<br /><br />';
        }

        return $result;
    }


}